<template>
  <div class="full-box padding-box shadow-box mine">
    <label>我的信息</label>
    <el-alert
      style="margin: 20px 0"
      type="info"
      effect="light"
      show-icon
      :closable="false"
    >
      <div>1.用户名（ 登录用的账号 ）无法修改</div>
      <div>2.如需需改所在部门，请联系相关系统管理员</div>
      <div>3.修改个人信息后将会退出系统</div>
    </el-alert>
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="150px"
      :inline="false"
      size="normal"
    >
      <el-form-item label="用户名" prop="">
        <el-input
          style="width: 300px"
          v-model="form.user_code"
          disabled
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="当前密码" prop="">
        <el-input
          type="password"
          style="width: 300px"
          placeholder="请输入当前密码"
          v-model="oldPwd"
        ></el-input>
      </el-form-item> -->

      <el-form-item label="用户昵称" prop="">
        <el-input
          style="width: 300px"
          v-model="form.user_name"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item label="当前部门" prop="">
        <el-input
          style="width: 300px"
          v-model="form.dept_name"
          disabled
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="当前密码" prop="oldPwd">
        <el-input
          type="password"
          style="width: 300px"
          placeholder="请输入当前密码"
          v-model="oldPwd"
        ></el-input>
      </el-form-item> -->
      <el-form-item label="新密码" prop="pwd1">
        <el-input
          show-password
          type="password"
          style="width: 300px"
          placeholder="请输入新密码"
          v-model="form.pwd1"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码验证" prop="pwd2">
        <el-input
          show-password
          type="password"
          style="width: 300px"
          placeholder="请再输入一次新密码"
          v-model="form.pwd2"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="$router.push('/home')">返回首页</el-button>
        <el-button type="primary" @click="changePwd">修改并退出</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { required } from '@/utils/eValidate';
import { postChangePwd } from '@/api/System/User';
import md5 from 'md5';
export default {
  name: 'mine',
  data() {
    let newPsw = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('密码不能为空'));
      }
      let psw = /(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{8,30}/;
      let check = psw.test(value);
      let pass = check;
      if (!pass) {
        callback(
          Error(
            '密码不正确，必须包含大小写字母、数字、特殊符号且长度在8-12位之间'
          )
        );
      } else {
        callback();
      }
    };
    return {
      form: { pwd1: '', pwd2: '', ...JSON.parse(localStorage.getItem('user')) },
      rules: {
        oldPwd: [required],
        pwd1: [
          {
            required: true,
            validator: newPsw
          }
        ],
        pwd2: [required]
      },
      oldPwd: '',
      pwd1: '',
      pwd2: ''
    };
  },
  methods: {
    changePwd() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.pwd1.trim() != this.form.pwd2.trim()) {
            this.$message.error('两次输入的密码不同');
            return;
          }
          postChangePwd({
            user_code: this.form.user_code,
            password: md5(this.form.pwd1 + 'saltbyzhuji2022')
          }).then((res) => {
            if (res.code == 0) {
              this.$message.success('密码修改成功,请重新登录！');
              this.$router.replace('/login');
              localStorage.clear();
              sessionStorage.clear();
            }
          });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.mine {
  > label {
    display: inline-block;
    border-left: 5px solid rgb(40, 100, 223);
    box-sizing: border-box;
    padding: 0 0 0 20px;
    font-size: 24px;
  }
}
.el-alert__description {
  > div {
    font-size: 18px;
    font-weight: 600;
    color: #000;
  }
}
</style>
